using Game.Main.Model;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class AlertorItem : BaseItem
	{
		private enum State
		{
			Close = 0,
			Open = 1
		}

		private State state;

		private GameObject red;

		private GameObject green;

		private int curIndex;

		private float waitTimer;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			red = model.Find("Red").gameObject;
			green = model.Find("Green").gameObject;
			SetState((State)data.state);
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (state == State.Open)
			{
				waitTimer += Time.deltaTime;
				if (waitTimer > 0.3f)
				{
					waitTimer = 0f;
					curIndex++;
					SetLampState(curIndex % 2 == 0);
				}
			}
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (state == State.Close)
			{
				SetState(State.Open);
				curIndex = 1;
				waitTimer = 0f;
				SetLampState(curIndex % 2 == 0);
			}
			else
			{
				SetState(State.Close);
				curIndex = 0;
				SetLampState(curIndex % 2 == 0);
			}
		}

		private void SetLampState(bool isRed)
		{
			red.SetActive(isRed);
			green.SetActive(!isRed);
		}

		private void SetState(State val)
		{
			if (state != val)
			{
				state = val;
				if (state == State.Open)
				{
					PlayDragSound();
				}
				else
				{
					StopDragSound();
				}
			}
		}

		public void SetModelEnabled(bool val)
		{
			if (model != null)
			{
				model.gameObject.SetActive(val);
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = (int)state;
		}
	}
}
